﻿using System;
using System.Collections.Generic;
using System.Text;
using FieldDevice.ModBus;
using FieldLib;
namespace FieldDevice.Devices
{
    class DeviceEM400:Device
    {
        public DeviceEM400(byte address)
            : base(DeviceCategory.PowerMeter, address)
        {
            Initialize();
        }

        byte[] RegistersValue = new byte[522];
        public ushort PT1=0;
        public ushort PT2 = 0;
        public ushort CT1 = 0;
        public ushort CT2 = 0;
        public int EP = 0;
        public int EQ = 0;

        public override void DebugReads()
        {
            if (DeviceManager.GlobalDeviceManager.ModBusIO.IsEmulation)
                this.UpdateReads();
            else
                base.DebugReads();
        }

        void Initialize()
        {
            base.RequestRegister(Emulation.EM400Emulator.ADDR_YX, 25);
        }

        public void UpdateReads()
        {
            base.ReadAndUpdateValue(Emulation.EM400Emulator.ADDR_YX, 25);
        }

        protected override float Translate(byte[] value, int begin, ushort registeraddress)
        {
            ushort tmpValue = value[begin];
            tmpValue <<= 8;
            tmpValue += value[begin + 1];
            switch (registeraddress)
            { 
                case Emulation .EM400Emulator.ADDR_YX:
                    if ((tmpValue & 0x0001) != 0)
                        base.RecordBooleanRead(OnOffValueName, true);
                    else
                        RecordBooleanRead(OnOffValueName, false);
                    if((tmpValue&0x0002)!=0)
                        RecordRead("DI2",1);
                    else
                        RecordRead("DI2",0);
                    if ((tmpValue & 0x0004) != 0)
                        RecordRead("DI3", 1);
                    else
                        RecordRead("DI3", 0);
                    if ((tmpValue & 0x0008) != 0)
                        RecordRead("DI4", 1);
                    else
                        RecordRead("DI4", 0);
                    break;
                case Emulation.EM400Emulator.ADDR_Vab:
                    RecordRead("Vab", tmpValue / 10.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_Vbc:
                    RecordRead("Vbc", tmpValue / 10.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_Vca:
                    RecordRead("Vca", tmpValue / 10.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_VA:
                    RecordRead("VA", tmpValue / 10.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_VB:
                    RecordRead("VB", tmpValue / 10.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_VC:
                    RecordRead("VC", tmpValue / 10.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_IA:
                    RecordRead("IA", tmpValue / 5.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_IB:
                    RecordRead("IB", tmpValue / 5.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_IC:
                    RecordRead("IC", tmpValue / 5.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_FR:
                    RecordRead("F", tmpValue / 100.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_PF:
                    RecordRead("PF", tmpValue / 100.0f);
                    break;
                case Emulation.EM400Emulator.ADDR_W:
                    RecordRead("P", tmpValue);
                    break;
                case Emulation.EM400Emulator.ADDR_Q:
                    RecordRead("Q", tmpValue);
                    break;
                case Emulation.EM400Emulator.ADDR_S:
                    RecordRead("S",tmpValue);
                    break;

                case Emulation.EM400Emulator.ADDR_EP_Hi:
                    EP = tmpValue;
                    EP <<= 8;
                    break;
                case Emulation.EM400Emulator.ADDR_EP_Low:
                    EP += tmpValue;
                    RecordRead("EPI", EP);
                    break;
                case Emulation.EM400Emulator.ADDR_EQ_Hi:
                    EQ = tmpValue;
                    EQ <<= 8;
                    break;
                case Emulation.EM400Emulator.ADDR_EQ_Low:
                    EQ += tmpValue;
                    RecordRead("EQL", EQ);
                    break;
                default:
                    break;
            }
            return 0.0f;
        }

    }
}
